package com.ehaoyao.config;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <dl>
 * <dt>RabbitMQConfig</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2006</dd>
 * <dd>CreateDate: 2020/5/12</dd>
 * </dl>
 *
 * @author zhangyp
 */
@Configuration
public class RabbitMQConfig {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public static final String Delay_Exchange_Name = "orderDelay";

    public static final String Timeout_Trade_Queue_Name = "zyporderDelayQueue2";

    public static final String Delay_routing_key = "orderDelay#";


    @Bean
    public Queue delayPayQueue() {
        return new Queue(RabbitMQConfig.Timeout_Trade_Queue_Name, true);
    }

    // 定义广播模式的延时交换机 无需绑定路由
    @Bean
    TopicExchange delayExchange(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "topic");
        TopicExchange topicExchange = new TopicExchange(RabbitMQConfig.Delay_Exchange_Name, true, false, args);
        topicExchange.setDelayed(true);
        return topicExchange;
    }

    // 绑定延时队列与交换机
    @Bean
    public Binding delayPayBind() {
        return BindingBuilder.bind(delayPayQueue()).to(delayExchange()).with(Delay_routing_key);
    }

    // 定义消息转换器
    @Bean
    Jackson2JsonMessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 定义消息模板用于发布消息，并且设置其消息转换器
    @Bean
    RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                logger.info("confirm=====> correlationData:{},ack:{},cause:{}", JSONObject.toJSONString(correlationData),b,s);
            }
        });


        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                logger.info("return=====> message:{},replyCode:{},replyText:{}", JSONObject.toJSONString(message),replyCode,replyText);
            }
        });
        return rabbitTemplate;
    }
    @Bean
    RabbitAdmin rabbitAdmin(final ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }



}
